import { user, userPhoto, userProfile, userProfilePatch } from '@/api'
import { RootThunkAction } from '@/types/store'
// 获取用户信息的 action
function profileAction (): RootThunkAction {
  return async dispatch => {
    const res = await user()
    // console.log(res)
    dispatch({ type: 'profile/user', payload: res.data.data })
  }
}
// 获取用户个人信息
function userProfileAction (): RootThunkAction {
  return async dispatch => {
    const res = await userProfile()
    console.log(res)
    dispatch({ type: 'user/userinfo', payload: res.data.data })
  }
}
// 修改用户个人信息
function setProfileAction (type: string, input: string): RootThunkAction {
  return async dispatch => {
    const res = await userProfilePatch(type, input)
    console.log(res)
    dispatch(userProfileAction())
  }
}
// 修改用户头像
function setPhotoAction (data: FormData): RootThunkAction {
  return async dispatch => {
    await userPhoto(data)
    dispatch(userProfileAction())
  }
}

export { profileAction, userProfileAction, setProfileAction, setPhotoAction }
